package com.sims.daoImp;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sims.bean.StudentPosition;
import com.sims.dao.IStudentPositionDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * StudentPosition entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.sims.bean.StudentPosition
 * @author MyEclipse Persistence Tools
 */

public class StudentPositionDAO extends HibernateDaoSupport implements IStudentPositionDAO {
	private static final Logger log = LoggerFactory
			.getLogger(StudentPositionDAO.class);
	// property constants
	public static final String SNO = "sno";
	public static final String POSITION = "position";
	public static final String CID = "cid";

	@Override
	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentPositionDAO#save(com.sims.bean.StudentPosition)
	 */
	@Override
	public void save(StudentPosition transientInstance) {
		log.debug("saving StudentPosition instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentPositionDAO#delete(com.sims.bean.StudentPosition)
	 */
	@Override
	public void delete(StudentPosition persistentInstance) {
		log.debug("deleting StudentPosition instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentPositionDAO#findById(java.lang.Integer)
	 */
	@Override
	public StudentPosition findById(java.lang.Integer id) {
		log.debug("getting StudentPosition instance with id: " + id);
		try {
			StudentPosition instance = (StudentPosition) getHibernateTemplate()
					.get("com.sims.bean.StudentPosition", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentPositionDAO#findByExample(com.sims.bean.StudentPosition)
	 */
	@Override
	public List findByExample(StudentPosition instance) {
		log.debug("finding StudentPosition instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentPositionDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding StudentPosition instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from StudentPosition as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentPositionDAO#findBySno(java.lang.Object)
	 */
	@Override
	public List findBySno(Object sno) {
		return findByProperty(SNO, sno);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentPositionDAO#findByPosition(java.lang.Object)
	 */
	@Override
	public List findByPosition(Object position) {
		return findByProperty(POSITION, position);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentPositionDAO#findByCid(java.lang.Object)
	 */
	@Override
	public List findByCid(Object cid) {
		return findByProperty(CID, cid);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentPositionDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all StudentPosition instances");
		try {
			String queryString = "from StudentPosition";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentPositionDAO#merge(com.sims.bean.StudentPosition)
	 */
	@Override
	public StudentPosition merge(StudentPosition detachedInstance) {
		log.debug("merging StudentPosition instance");
		try {
			StudentPosition result = getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentPositionDAO#attachDirty(com.sims.bean.StudentPosition)
	 */
	@Override
	public void attachDirty(StudentPosition instance) {
		log.debug("attaching dirty StudentPosition instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentPositionDAO#attachClean(com.sims.bean.StudentPosition)
	 */
	@Override
	public void attachClean(StudentPosition instance) {
		log.debug("attaching clean StudentPosition instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IStudentPositionDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (IStudentPositionDAO) ctx.getBean("StudentPositionDAO");
	}
	
	public List findStudentStudentPositionByCid(Object cid) {
		String queryString = "select new com.sims.beanEnhanced.StudentStudentPosition(s,sp) " 
				+ "from Student s, StudentPosition sp where "
				+ "s.sno = sp.sno and "
				+ "s.cid = :scid";
		org.hibernate.Query query = getSession().createQuery(queryString);
		query.setInteger("scid", (Integer) cid);
		return query.list();
	}
}